package view.mt4j.widget;

import static app.ViewConstants.COLOR_ORANGE;

import model.util.Dimension;

import org.mt4j.MTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;

import processing.core.PApplet;

import view.mt4j.util.ProcessorHelper;

public class MTModalPopup extends MTRectangle {

	private static final float BUTTON_Y = 40f;

	public static void showPopup(MTApplication mtApplication, MTRectangle topContainer, String message) {
		topContainer.addChild(new MTModalPopup(
				mtApplication,
				new Dimension(
						topContainer
								.getWidthXY(TransformSpace.LOCAL),
						topContainer
								.getHeightXY(TransformSpace.LOCAL)),
				message));
	}
	
	public MTModalPopup(PApplet mtApplication, Dimension dim,
			String message) {
		super(mtApplication, dim.width, dim.height);
		setFillColor(new MTColor(MTColor.GRAY.getR(), MTColor.GRAY.getG(),
				MTColor.GRAY.getB(), MTColor.ALPHA_HALF_TRANSPARENCY));
		MTRectangle middleZone = new MTRectangle(mtApplication, 400, 200);
		middleZone.setFillColor(MTColor.WHITE);
		middleZone.setStrokeColor(MTColor.BLACK);
		middleZone.setStrokeWeight(3f);
		middleZone.setPositionRelativeToParent(new Vector3D(dim.width / 2f,
				dim.height / 2f));
		addChild(middleZone);
		
		addMessage(mtApplication, message, middleZone);
		
		addOkButton(mtApplication, middleZone);
	}

	private void addMessage(PApplet mtApplication,
			String message, MTRectangle middleZone) {
		MTTextArea messageTextArea = new MTTextArea(mtApplication, FontManager
				.getInstance().createFont(mtApplication, "arial.ttf", 20,
						MTColor.BLACK));
		messageTextArea.setNoFill(true);
		messageTextArea.setNoStroke(true);
		messageTextArea.setText(message);

		middleZone.addChild(messageTextArea);
		messageTextArea.setPositionRelativeToParent(new Vector3D(middleZone
				.getWidthXY(TransformSpace.LOCAL) / 2f, middleZone
				.getHeightXY(TransformSpace.LOCAL) / 2f - BUTTON_Y));
		messageTextArea.removeAllGestureEventListeners();
	}

	private void addOkButton(PApplet mtApplication,
			MTRectangle middleZone) {
		MTRoundRectangle okButtonShape = new MTRoundRectangle(mtApplication, 0,
				0, 0, 100, 80, 10, 10);
		okButtonShape.setPositionRelativeToParent(new Vector3D(middleZone
				.getWidthXY(TransformSpace.LOCAL) / 2f, middleZone
				.getHeightXY(TransformSpace.LOCAL) / 2f + BUTTON_Y));
		okButtonShape.setFillColor(COLOR_ORANGE);

		middleZone.addChild(okButtonShape);

		MTTextArea okButtonText = new MTTextArea(mtApplication, FontManager
				.getInstance().createFont(mtApplication, "arial.ttf", 50,
						MTColor.WHITE));
		okButtonText.setNoFill(true);
		okButtonText.setNoStroke(true);
		okButtonText.setText("OK");

		okButtonShape.addChild(okButtonText);
		okButtonText.setPositionRelativeToParent(new Vector3D(okButtonShape
				.getWidthXY(TransformSpace.LOCAL) / 2f, okButtonShape
				.getHeightXY(TransformSpace.LOCAL) / 2f));
		okButtonText.removeAllGestureEventListeners();
		ProcessorHelper.delegateDragScaleRotateEventTo(okButtonShape,
				okButtonText);
		okButtonShape.removeAllGestureEventListeners();
		ProcessorHelper.delegateDragScaleRotateEventTo(middleZone,
				okButtonShape);

		okButtonText.registerInputProcessor(new TapProcessor(mtApplication));
		okButtonShape.registerInputProcessor(new TapProcessor(mtApplication));
		ProcessorHelper.delegateGestureEventTo(okButtonShape,
				TapProcessor.class, okButtonText);

		okButtonShape.addGestureListener(TapProcessor.class,
				new IGestureEventListener() {

					@Override
					public boolean processGestureEvent(MTGestureEvent ge) {
						TapEvent te = (TapEvent) ge;
						if (te.getId() == TapEvent.GESTURE_ENDED
								&& te.isTapped()) {
							removeFromParent();
						}
						return false;
					}
				});
	}
}
